<!doctype html>
<html lang="zh-cmn-Hans">
<!--
 * DefaultGatewaysRead.html
 * 用户列表
 * @package Console
 * @author SunP<sunp@aowsp.com>
 * @copyright Copyright © Since 2012 SunP All Rights Reserved.
-->
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
	<meta name="renderer" content="webkit"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
	<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
	<meta name="author" content="SunP,sunp@aowsp.com,SunQuan,sunquan@onlyedu.com"/>
	<link rel="stylesheet" href="<{$Frontend}>/Javascript/layui-v2.2.2/css/layui.css" media="all">
	<link rel="stylesheet" href="<{$Frontend}>/font-awesome/css/font-awesome.min.css" media="all">
	<link rel="stylesheet" href="<{$Frontend}>/Console/build/css/app.css" media="all">
	<link rel="stylesheet" href="<{$Frontend}>/Console/build/css/themes/orange.css" media="all">
	<style>
		.layui-table-view {
			margin: 0;
		}
	</style>
</head>
<body class="kit-theme">
<div class="layui-form">
	<div class="layui-form-item" style="padding:10px 0 0 10px;margin-bottom: 10px">
		<div class="layui-input-inline">
			<input name="Phone" required lay-verify="required" placeholder="请输入电话" autocomplete="off" id="IndexPhone"
				   class="layui-input">
		</div>

		<div class="layui-input-inline">
			<button name="SPSubmit" lay-submit lay-filter="AddFormButton" class="layui-btn layui-btn-normal"
					value="1344" id="test">
				新增
			</button>
		</div>
	</div>
</div>
<table class="List" id="List" lay-filter="List"></table>
<script type="text/html" id='AddNewStuTpl'>
	<div style="margin:10px">
		<div class="layui-form" lay-filter="AddForm">
			<div class="layui-form-item">
				<label class="layui-form-label" style="text-align: left">学员姓名</label>
				<div class="layui-input-block">
					<input type="text" name="Form[Customer][CnName]" required lay-verify="required"
						   placeholder="请输入学员姓名" autocomplete="off" class="layui-input" value="{{d.CnName}}">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label" style="text-align: left">学员电话</label>
				<div class="layui-input-block">
					<input type="text" name="Form[Customer][Phone]" required lay-verify="required"
						   placeholder="请输入学员电话" autocomplete="off" class="layui-input" value="{{d.Phone}}">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label" style="text-align: left">选择年级</label>
				<div class="layui-input-block" id="Class" required></div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label" style="text-align: left">选择校区</label>
				<div class="layui-input-block" id="School" required></div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label" style="text-align: left">流类型</label>
				<div class="layui-input-block" id="Type" lay-filter="Type" required></div>
			</div>
			<label class="layui-form-label" id="SuccessHint" style="text-align: left;color:#888888;width: 160px;float: none"></label>
			<div class="layui-form-item" style="display: none">
				<label class="layui-form-label" style="text-align: left">介绍人</label>
				<div class="layui-input-block">
					<div class="layui-inline" style="width: 100%">
						<input type="text" name="Form[OuterOwner]" class="layui-input" id="InnerOwner">
					</div>
				</div>
			</div>
			<div class="layui-form-item" style="display: none">
				<div class="layui-input-inline" style="margin-right: 0;float: right">
					<button name="SPSubmit" lay-submit lay-filter="AddButton" class="layui-btn layui-btn-normal">新增
					</button>
					<button class="layui-btn layui-btn-normal" id="AddCancel">取消</button>
				</div>
			</div>
			<div id="OldStu"></div>
		</div>
	</div>
</script>
<script type="text/html" id="ClassSelectTpl">
	{{#  layui.each(d, function(index, item){ }}
	{{# if(item.Checked == 1){ }}
	<input type="radio" name="Form[Grade]" value='{{item.UUID}}' title='{{item.CnName}}' checked>
	{{# }else{ }}
	<input type="radio" name="Form[Grade]" value='{{item.UUID}}' title='{{item.CnName}}'>
	{{# } }}
	{{#  }); }}
</script>
<script type="text/html" id="SchoolSelectTpl">
	{{#  layui.each(d, function(index, item){ }}
	{{# if(item.Checked == 1){ }}
	<input type="radio" name="Form[Group]" value='{{item.UUID}}' title='{{item.CnName}}' checked>
	{{# }else{ }}
	<input type="radio" name="Form[Group]" value='{{item.UUID}}' title='{{item.CnName}}'>
	{{# } }}
	{{#  }); }}
</script>
<script type="text/html" id="TypeSelectTpl">
	{{#  layui.each(d, function(index, item){ }}
	{{# if(item.Checked == 1){ }}
	<input type="radio" name="Form[Type]" value='{{item.UUID}}' title='{{item.CnName}}' lay-filter="Type" checked>
	{{# }else{ }}
	<input type="radio" name="Form[Type]" value='{{item.UUID}}' title='{{item.CnName}}' lay-filter="Type">
	{{# } }}
	{{#  }); }}
</script>
<script type="text/html" id="IntroTpl">
	<div class="layui-form-item" id="AddNewDiv" style="float: left;margin-left: 15px;margin-top: 15px">
		<div class="layui-input-inline">
			<input required lay-verify="required" placeholder="请输入介绍人电话" autocomplete="off" class="layui-input"
				   id="InnerPhoneInput">
		</div>
		<div class="layui-input-inline" style="width: auto;margin-right: 0">
			<button class="layui-btn layui-btn-normal" id="AddNew">校验</button>
		</div>
	</div>
	<label class="layui-form-label" id="JiaoYanLable"
		   style="text-align: left;color:#888888;width: 160px;float: none"></label>
</script>
<script type="text/html" id="Bar">
	<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="EditBar">跟进</a>
</script>

<script src="<{$Frontend}>/Javascript/jquery-3.2.1.min.js"></script>
<script src="<{$Frontend}>/Javascript/layui-v2.2.2/layui.js"></script>
<script src="<{$Frontend}>/Console/build/common/common.js"></script>

<script>
	$.ajax({url: "<{SPURL c = 'CRM' a = 'FlowsRead' SPSubmit=''}>"}).done(function (FlowsReadData) {
		FlowsReadData = JSON.parse(FlowsReadData);
		if (FlowsReadData.Status == 1) {
			var TypeIDArray = [],GroupIDArray = [],ExecutorIDArray = [];
			for (var i in FlowsReadData.Data) {
				TypeIDArray.push(FlowsReadData.Data[i].Type);
				GroupIDArray.push(FlowsReadData.Data[i].Group);
				ExecutorIDArray.push(FlowsReadData.Data[i].Executor);
			}
			$.when($.ajax({url: "<{SPURL c = 'Access' a = 'UsersRead' SPSubmit=''}>", data: {Form: {UUID: ExecutorIDArray}}}),
				$.ajax({url: "<{SPURL c = 'Access' a = 'TagsRead' SPSubmit=''}>", data: {Form: {UUID: TypeIDArray}}}),
				$.ajax({url: "<{SPURL c = 'Access' a = 'TagsRead' SPSubmit=''}>", data: {Form: {UUID: GroupIDArray}}})
			).then(function (Executs, Types, Groups) {
				Executs = JSON.parse(Executs[0]);
				Types = JSON.parse(Types[0]);
				Groups = JSON.parse(Groups[0]);

				var TypeCnArray = {};
				for(var a=0;a<Types.Data.length;a++){
					TypeCnArray[Types.Data[a].UUID] = Types.Data[a];
				}
				for(var a=0;a<FlowsReadData.Data.length;a++){
					if(FlowsReadData.Data[a].Type){
						var b = FlowsReadData.Data[a].Type;
						FlowsReadData.Data[a].TypeName = TypeCnArray[b].CnName;
					}
				}

				var GroupsCnArray = {};
				for(var a=0;a<Groups.Data.length;a++){
					GroupsCnArray[Groups.Data[a].UUID] = Groups.Data[a];
				}
				for(var a=0;a<FlowsReadData.Data.length;a++){
					if(FlowsReadData.Data[a].Group){
						var b = FlowsReadData.Data[a].Group;
						FlowsReadData.Data[a].GroupName = GroupsCnArray[b].CnName;
					}
				}

				var ExecutsCnArray = {};
				for(var a=0;a<Executs.Data.length;a++){
					ExecutsCnArray[Executs.Data[a].UUID] = Executs.Data[a];
				}
				for(var a=0;a<FlowsReadData.Data.length;a++){
					if(FlowsReadData.Data[a].Executor){
						var b = FlowsReadData.Data[a].Executor;
						FlowsReadData.Data[a].ExecutsName = ExecutsCnArray[b].CnName;
					}
				}

				layui.use(['layer', 'table', 'form', 'laytpl', 'laydate'], function () {
					var layer = layui.layer,//弹层
						table = layui.table,//表格
						$ = layui.jquery,
						laytpl = layui.laytpl,
						form = layui.form,
						laydate = layui.laydate;
					form.render();

					table.render({
						elem: '#List'
						, height: $(window).height() - 58
						, limit: 1000
						, data: FlowsReadData.Data
						, cols: [[
							{field: 'UUID', title: '识别码',width:"20%"}
							, {field: 'CnName', title: '姓名',width:"10%"}
							, {field: 'Phone', title: '客户手机',width:"12%"}
							, {field: 'GroupName', title: '校区',width:"20%"}
							, {field: 'TypeName', title: '类型',width:"20%"}
							, {field: 'ExecutsName', title: '执行人',width:"10%"}
							, {fixed: 'right', title: "操作栏", toolbar: '#Bar',width:"8%"}
						]]
						, id: "TableId"
					});
					form.on('submit(AddFormButton)', function (data) {
						var PhoneNum = data.field.Phone;
						if (CheckPhone("IndexPhone")) {
						} else {
							return false;
						}
						$.ajax({
							url: "<{SPURL c = 'CRM' a = 'CustomersRead' SPSubmit=''}>",
							data: {
								Form: {Phone: PhoneNum}
							},
							success: function (data) {
								data = JSON.parse(data);
								if (data.Status == "1") {
									if (data.Data.length != 0) {

									} else {
										data.Data[0] = {
											"UUID": "",
											"CnName": "",
											"Phone": PhoneNum,
											"Time": ""
										};
									}
									laytpl($("#AddNewStuTpl").html()).render(data.Data[0], function (html) {
										var AddLayer = layer.open({
											type: 1,
											title: '新增',
											content: html,
											area: ['80%', "80%"],
											btn: ['提交', '取消'],
											yes: function (index, layero) {
												form.on('submit(AddButton)', function (data) {
													CreatData2(data.field, "<{SPURL c = 'CRM' a = 'FlowCreat' SPSubmit=''}>");
												});
												$('div[lay-filter="AddForm"]').find('button[lay-submit]').click();
											},
											success: function () {
												laydate.render({
													elem: '#TimeSelect',
													type: "date"
												});
												$.when(
													$.ajax({
														url: "<{SPURL c = 'Access' a = 'TagsTreesRead' SPSubmit=''}>",
														data: {Form: {UUID: ["1d46kqne893b155e"]}}
													}),
													$.ajax({
														url: "<{SPURL c = 'Access' a = 'TagsTreesRead' SPSubmit=''}>",
														data: {Form: {UUID: ["1d4959nad4b0efc4"]}}
													}),
													$.ajax({
														url: "<{SPURL c = 'Access' a = 'TagsTreesRead' SPSubmit=''}>",
														data: {Form: {UUID: ["1d46jpta8453f59e"]}}
													})
												).then(function (Classes, Schools, Types) {
													Classes = JSON.parse(Classes[0]);
													Classes.Data.splice(0, 1);

													Schools = JSON.parse(Schools[0]);
													for (var i in Schools.Data) {
														Schools.Data[i].CnName = Schools.Data[i].CnName.replace("校区", "");
													}
													Schools.Data.splice(0, 1);

													Types = JSON.parse(Types[0]);
													Types.Data.splice(0, 1);

													laytpl($('#ClassSelectTpl').html()).render(Classes.Data, function (html) {
														$("#Class").append(html);
														form.render('radio');
													});
													laytpl($('#SchoolSelectTpl').html()).render(Schools.Data, function (html) {
														$("#School").append(html);
														form.render('radio');
													});
													laytpl($('#TypeSelectTpl').html()).render(Types.Data, function (html) {
														$("#Type").append(html);
														form.render('radio');
													});
													form.on('radio(Type)', function (data) {
														if (data.value == "1d46jqna166a7714") {
															laytpl($("#IntroTpl").html()).render({}, function (html) {
																var IntroLayer = layer.open({
																	type: 1,
																	title: '校验转介绍',
																	content: html,
																	area: ['40%', "30%"],
																	btn: ['取消'],
																	yes: function (index, layero) {
																		layer.close(IntroLayer);
																	},
																	success: function () {
																		var Inner = $("#InnerPhoneInput").val();
																		$("#AddNew").click(function () {
																			var Inner = $("#InnerPhoneInput").val();
																			$.ajax({
																				url: "<{SPURL c = 'CRM' a = 'CustomersRead' SPSubmit=''}>",
																				data: {Form: {Phone: Inner}}
																			}).done(function (Inner) {
																				Inner = JSON.parse(Inner);
																				if (Inner.Status == 1) {
																					if (Inner.Data.length == 0) {
																						$("#JiaoYanLable").text("此用户非老生，无法做转介绍");
																					} else if (Inner.Data.length == 1) {
																						$("#SuccessHint").text("此转介绍标记为" + Inner.Data[0].CnName + "所有");
																						layer.close(IntroLayer);
																						$("#InnerOwner").val(Inner.Data[0].UUID);
																					}
																				}
																			});
																		});
																	}
																});
															})
														}
													})
												});
											}
										});
									});
								} else {
									layer.msg(data.Data);
								}
							}
						});
					});
				});
			});
		} else {
			console.log(FlowsReadData.Data);
			/*window.location.href = "http://console.aowsp.com/Access-SignIn";*/
		}
	});
</script>
</body>
</html>
